package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohm_time_op.entity.dto.MachineDTO;
import cn.lystudio.rohm_time_op.entity.po.MachinePO;
import cn.lystudio.rohm_time_op.entity.po.QrTypePO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.QrTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/qrType")
@Slf4j
public class QrTypeController {
    private QrTypeService qrTypeService;

    @Autowired
    @Lazy
    public void setQrTypeService(QrTypeService qrTypeService) {
        this.qrTypeService = qrTypeService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<QrTypePO> all = qrTypeService.getAll();
        if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);

        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer qrTypeID, Boolean enabled) {
        return SaResult.data(qrTypeService.uptQrTypeEnabled(qrTypeID, enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delQrType")
    public SaResult delQrType(Integer qrTypeID){
        return SaResult.data(qrTypeService.delQrType(qrTypeID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addQrType")
    public SaResult addQrType(@RequestBody QrTypePO qrTypePO){
        return SaResult.data(qrTypeService.addQrType(qrTypePO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptQrType")
    public SaResult uptQrType(@RequestBody QrTypePO qrTypePO){
        return SaResult.data(qrTypeService.uptQrType(qrTypePO));
    }

    @GetMapping("/getAllQrTypeName")
    public SaResult getAllQrTypeName(){
        List<QrTypePO> qrTypePOList = qrTypeService.getAll();
        CollUtil.filter(qrTypePOList,qrTypePO -> {
            return qrTypePO.getEnabled();
        });
        return SaResult.data(qrTypePOList);
    }

}
